package com;

import com.linar.jintegra.NTLMAuthenticate;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 * 
 * @author mona.chandrapal
 */
public class LoginChecks {

	public boolean loginvalidation(String username, String password) {
		boolean success = false;
		try {
			NTLMAuthenticate.validate("172.25.11.251", "corp", username,
					password);
			success = true;

		} catch (SecurityException ex) {
			success = false;
			System.out.println("User not authenticated");
			// Logger.getLogger(LoginChecks.class.getName()).log(Level.SEVERE,
			// null, "User not authenticated:" + ex);
		} catch (IOException ex) {
			// Logger.getLogger(LoginChecks.class.getName()).log(Level.SEVERE,
			// null, "IO Exception:" + ex);
			System.out.println("IO exception");
		} catch (Exception ex) {
			// Logger.getLogger(LoginChecks.class.getName()).log(Level.SEVERE,
			// null, "Other exception:" + ex);
			System.out.println("Other exception");
		}

		// AppLogger logger = AppLogger.getLogger(AppLogger.class);

		try {
			// AppLogger.setUpLogger();
		} catch (Exception e) {
			// Logger.getLogger(LoginChecks.class.getName()).log(Level.SEVERE,
			// null, ex);
			System.out.println("Exception when reading properties file");
		}

		Exception testException = new RuntimeException(
				"This is a test exception");
		/*
		 * logger.fatal("Test fatal"); logger.fatal("Test fatal",
		 * testException); logger.error("Test error"); logger.error("Test
		 * error", testException); logger.warn("Test warn"); logger.warn("Test
		 * warn", testException); logger.info("Test info"); logger.debug("Test
		 * debug");
		 */

		return (success);

	}

	public String accessrightsvalidation(String username) {
		Root root = new Root();
		ResultSet rs = null;
		String sql, sql1 = "";
		PreparedStatement pst1 = null;
		PreparedStatement pst = null;

		//System.out.println("Access right validation");
		String userrole = "9";
		try {
			root.initConnection();
			sql1 = "select userrole from corpusers where corpusername= ?;";
			pst1 = root.con.prepareStatement(sql1);
			pst1.setString(1, username);
			//System.out.println(pst1);

			rs = pst1.executeQuery();

			while (rs.next()) {
				userrole = rs.getString("userrole");
			}
			if (userrole == "9") {
				sql = "insert into corpusers (corpusername)  values  (?);";
				pst = root.con.prepareStatement(sql);
				pst.setString(1, username);
				pst.executeUpdate();
				userrole = "0";
			}
			//System.out.println("userrole: " + userrole);
		} catch (Exception e) {
			System.out.print("Error validating access rights: " + e.toString());
		} finally {
			
			try {
				rs.close();
				pst1.close();
				pst.close();
				root.closeConnection();
			} catch (Exception e) {

			}

		}
		return (userrole);
	}
	
	
	public void setuser(String username) {
		Root root = new Root();
		String sql1 = "";
		PreparedStatement pst1 = null;
		
		try {
			root.initConnection();
			sql1 = "insert into loggedusers (user) values (? );";
			pst1 = root.con.prepareStatement(sql1);
			pst1.setString(1, username);
			//System.out.println(pst1);
			pst1.executeUpdate();
		} catch (Exception e) {
			System.out.print("Error logging user: " + e.toString());
		} finally {
			
			try {
				pst1.close();
				root.closeConnection();
			} catch (Exception e) {

			}

		}
		
	}
	

}
